package com.psa.system;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.google.gson.reflect.TypeToken;
import com.psa.adapter.SqlObjectStatusListAdapter;
import com.psa.element.CustomerVisit;
import com.psa.element.QuerySetting;
import com.psa.element.SqlObjectStatus;
import com.psa.tool.ExitApplication;
import com.psa.tool.IGeneralEventCallback;
import com.psa.tool.RequestManager;
import com.psa.tool.SystemStatus;
import com.psa.tool.net.DataUpdateManager;
import com.wxw.BaseLibrary.tool.ActivityTool;
import net.duohuo.dhroid.ioc.InjectUtil;
import net.duohuo.dhroid.ioc.annotation.InjectView;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author david
 * @date 2014-06-09
 */
public class UpdateDataActivity extends BaseActivity {
    @InjectView(id = R.id.lv_update_items)
    private ListView lv_update_items;
    @InjectView(id = R.id.btn_reset, click = "onClick")
    private Button btn_reset;
    @InjectView(id = R.id.btn_ok, click = "onClick")
    private Button btn_ok;

    private List<SqlObjectStatus> updateItems;
    private List<SqlObjectStatus> for_update = new ArrayList<SqlObjectStatus>();
    private SqlObjectStatusListAdapter adapter;
    private String CallbackMsg = "";
    private String completedMsg = "";

    private DataUpdateManager dataUpdateManager = null;

    private Runnable HttpCallbackCompleted = new Runnable() {
        @Override
        public void run() {
            for (SqlObjectStatus sqlObjectStatus : updateItems){
                sqlObjectStatus.checked = false;
            }
            adapter.notifyDataSetChanged();
            SystemStatus.addMessage(completedMsg);
//            SystemStatus.showMessage(UpdateDataActivity.this, completedMsg);
        }
    };

    private Runnable showHttpCallback = new Runnable() {
        @Override
        public void run() {
            SystemStatus.addMessage(CallbackMsg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTool.hideTitle(this);
        ActivityTool.fullScreen(this);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.updatedata_layout);
        InjectUtil.inject(this);
        btn_next.setVisibility(View.INVISIBLE);
        CallbackMsg = getString(R.string.update_data_complated);
        dataUpdateManager = new DataUpdateManager(this);
        dataUpdateManager.setMessage(getString(R.string.update_data_dialog_text));
        updateItems = DataUpdateManager.createUpdateItems();
        adapter = new SqlObjectStatusListAdapter(this, updateItems);
        tv_head_title.setText(getString(R.string.update_data_title));
        lv_update_items.setAdapter(adapter);

        dataUpdateManager.setOnUpdateCompletedListener(new IGeneralEventCallback() {
            @Override
            public void finished(Object sender, String eventContent) {
                completedMsg = eventContent;
                UpdateDataActivity.this.runOnUiThread(HttpCallbackCompleted);
            }
        });
        dataUpdateManager.setOnUpdateTimeoutListener(new IGeneralEventCallback() {
            @Override
            public void finished(Object sender, String eventContent) {
                CallbackMsg = eventContent;
                UpdateDataActivity.this.runOnUiThread(showHttpCallback);
            }
        });
        dataUpdateManager.setOnUpdatingListener(new IGeneralEventCallback() {
            @Override
            public void finished(Object sender, String eventContent) {
                CallbackMsg = eventContent;
                UpdateDataActivity.this.runOnUiThread(showHttpCallback);
            }
        });

        tv_info.setText(SystemStatus.getReqCount());
    }


    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.btn_ok){
            for_update.clear();
            for (SqlObjectStatus item : updateItems){
                if (item.checked) {
                    for_update.add(item);
                }
            }
            if (for_update.size() <= 0){
                SystemStatus.showMessage(UpdateDataActivity.this, getString(R.string.update_data_nothing_selected));
                return;
            }
            SqlObjectStatus sqlObjectStatus = new SqlObjectStatus();
            sqlObjectStatus.checked = true;
            sqlObjectStatus.iSqlObject = new QuerySetting();
            sqlObjectStatus.type = new TypeToken<List<QuerySetting>>(){}.getType();
            for_update.add(sqlObjectStatus);
            sqlObjectStatus = new SqlObjectStatus();
            sqlObjectStatus.checked = true;
            sqlObjectStatus.iSqlObject = new CustomerVisit();
            sqlObjectStatus.type = new TypeToken<List<CustomerVisit>>(){}.getType();
            for_update.add(sqlObjectStatus);
            dataUpdateManager.setForUpdateItems(for_update);
            dataUpdateManager.start();
        } else if (view.getId() == R.id.btn_reset){
            for (SqlObjectStatus sqlObjectStatus : updateItems){
                sqlObjectStatus.checked = false;
            }
            adapter.notifyDataSetChanged();
        } else {
            RequestManager.getRequestManager().removeGeneralEventListener(this);
            setResult(0);
            finish();
        }
    }
}
